package com.metagurukul.metagranth.service;

import java.sql.SQLException;
import java.util.ArrayList;

import junit.framework.TestCase;

import com.metagurukul.metagranth.vo.Question;
import com.metagurukul.metagranth.vo.Tag;

public class TagJUnitTEST extends TestCase {

	/**
	 * this function test the QuestionService class about positive case of
	 * getLatestQuestions function(the questions are present in the databases
	 * more than one and it returns the arraylist of metadata of question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first
	 * 
	 * expected result:function should return added questions on the top actual
	 * result :function returns true
	 * 
	 * @author rohit
	 * 
	 */

	public void testGetTagListPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// service class method
			ArrayList<Tag> tagList = TagService.getTagsList();
			// size of all tags
			int preSize = tagList.size();
			// add question to database(it will add some tags also to tag table)
			QuestionService.askQuestion(newQuestion, newTag);
			// get list of all tag
			tagList = TagService.getTagsList();
			// get size
			int postSize = tagList.size();
			// difference should be 1.
			assertEquals(1, postSize - preSize);

		} catch (Exception e) {
			e.printStackTrace();
			fail();
			
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}
	}
	/**
	 * this function test the QuestionService class about positive case of
	 * getTagCount function(the questions are present in the databases
	 * more than one and it returns count of tags present in the database which are similar to given 
	 * tag)
	 * 
	 * secnario:database is having some questions already and we add 1 test
	 * question here first and a test tag.
	 * 
	 * expected result:function should return right count.
	 * result :function returns 0 at first attempt and after adding tag it should return 1.
	 * 
	 * @author rohit
	 * 
	 */
/*
	public void testGetTagCountPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// service class method
			int tagCount = TagService.getTagCount(newTag);
			assertEquals(tagCount,0);
			// add question to database(it will add some tags also to tag table)
			QuestionService.askQuestion(newQuestion, newTag);
			// get tag count
			tagCount = TagService.getTagCount(newTag);
			assertEquals(tagCount, 0);

		} catch (Exception e) {
			e.printStackTrace();
			fail();
			
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}
	}
	*/
	
	
	/**
	 * this function test the QuestionService class about positive case of
	 * getTagNameList function(the tags are present in the databases
	 * more than one and it returns all tags  present in the database which are similar to given 
	 * String.)
	 * 
	 * secnario:database is having some questions already and we add 1 test
	 * tag here first.
	 * 
	 * expected result:function should return some no of tags first and after adding tags
	 * it will return 1 more than previous size.
	 * result :function returns preSize at first attempt and after adding tag it should return 1 more than presize .
	 * 
	 * @author rohit
	 * 
	 */

	public void testGetTagNameListPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// service class method
			ArrayList<String> list = (ArrayList<String>) TagService.getTagNameList(newTag.getTagName());
			int preSize=list.size();
			// add question to database(it will add some tags also to tag table)
			QuestionService.askQuestion(newQuestion, newTag);
			// service class method
			 list = (ArrayList<String>) TagService.getTagNameList(newTag.getTagName());
			int postSize=list.size();
			assertEquals(1, postSize-preSize);

		} catch (Exception e) {
			e.printStackTrace();
			fail();
			
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}
	}
	

	
}
